<template>
	<view class="bg-white">
		<view class="login" v-if="type=='login'">
			<u-field label="新密码" password placeholder="" v-model="pwd"></u-field>
			<u-field label="确认密码" password placeholder="" v-model="pwd_confirmation"></u-field>
			<u-field label="验证码" placeholder="" v-model="code">
				<u-tag slot="right" mode="plain" :text="tips" type="success" @click="getCode"></u-tag>
			</u-field>
			<u-verification-code :seconds="60" ref="uCode" @change="codeChange"></u-verification-code>
		</view>
		
		<view class="login" v-else>
			<u-field label="新密码" password placeholder="" maxlength="6" v-model="pwd"></u-field>
			<u-field label="确认密码" password placeholder="" maxlength="6" v-model="pwd_confirmation"></u-field>
			<u-field label="验证码" placeholder="" v-model="code">
				<u-tag slot="right" mode="plain" :text="tips" type="success" @click="getCode"></u-tag>
			</u-field>
			<u-verification-code :seconds="60" ref="uCode" @change="codeChange"></u-verification-code>
		</view>
		
		<my-btn gold style="margin: 60rpx auto;" @click="cgPwd">修改</my-btn>
		<u-gap height="10"></u-gap>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: '',
				pwd: '',
				pwd_confirmation: '',
				code: '',
				tips: '获取验证码',
			};
		},
		computed: {
			typeText() {
				if (this.type == 'login') {
					return '登录'
				} else if (this.type == 'trade') {
					return '交易'
				}
			}
		},
		onLoad(opt) {
			this.type = opt.type
			uni.setNavigationBarTitle({
				title: `修改${this.typeText}密码`
			})
		},
		methods: {
			codeChange(e) {
				this.tips = e
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					uni.showLoading({
						title: '正在获取验证码'
					})
					this.$u.post('/code/mobile?type=' + this.type).then(res => {
						uni.hideLoading();
						if (res.data.code == 0) {
							this.$u.toast('验证码已发送');
							this.$refs.uCode.start();
						} else {
							this.$u.toast(res.data.msg)
						}
					})
				} else {
					this.$u.toast('稍后再试');
				}
			},
			cgPwd() {
				let data = {
					pwd: this.pwd,
					pwd_confirmation: this.pwd_confirmation,
					type: this.type,
					code: this.code
				}
				this.$u.post('/member/safe/pwd', data).then(res => {
					this.$msg(res.data.msg)
				})
			}
		}
	}
</script>

<style lang="scss">

</style>
